<template>
  <div>
    <input type="file" @change="upload" />
  </div>
</template>
<script>
export default {
  name: 'Doc',
  data () {
    return {
      // value1: ''
    }
  },
  methods: {
    upload (e) {
      const _this = this
      const files = e.target.files || e.dataTransfer.files
      // this.value
      debugger
      //支持chrome IE10 ff
      if (window.FileReader) {
        const file = files[0];
        const filename = file.name.split(".")[0];
        const reader = new FileReader();
        reader.onload = function () {
          const res = this.result
          // console.log(this.result);
          // console.log(_this.getBase64(this.result))
          debugger
          const blob = new Blob([res], { type: "application/msword;charset=utf-8" });
          debugger
          let url = window.URL.createObjectURL(blob);
          if (window.navigator.msSaveOrOpenBlob) { // 兼容IE10
            navigator.msSaveBlob(blob, filename)
          } else { // 其他非IE内核支持H5的浏览器
            let downloadElement = document.createElement("a");
            downloadElement.href = url;
            downloadElement.download = filename;
            document.body.appendChild(downloadElement);
            downloadElement.click();
            document.body.removeChild(downloadElement);
            window.URL.revokeObjectURL(url);
          }
        };
        reader.readAsText(file);
      }
      //支持IE 7 8 9 10
      else if (typeof window.ActiveXObject != 'undefined') {
        var xmlDoc;
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.load(files);
        console.log(xmlDoc.xml);
      }
      //支持FF
      else if (document.implementation && document.implementation.createDocument) {
        var xmlDoc;
        xmlDoc = document.implementation.createDocument("", "", null);
        xmlDoc.async = false;
        xmlDoc.load(files);
        console.log(xmlDoc.xml);
      } else {
        console.error('error');
      }
    },
    getBase64 (file) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result);
        reader.onerror = error => reject(error);
      });
    }
  }
}
</script>